<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/2
 * Time: 15:49
 */
namespace app\home\controller;
use app\data\Data;

/**
 * +----------------------------------
 * Class Index
 * @package app\home\controller
 * 网站前台首页
 * +----------------------------------
 */
class Index extends Base{
    protected $tag;
    protected $article_top;
    protected $type;
    protected $reading_article;
    protected $config;

    /**
     * 构造函数，获取通用数据
     */
    public function _initialize(){
        $this->tag         = self::getTag();                  //获取标签
        $this->reading_article = self::getReadingArticle();    //获取阅读量最多的文章
        $this->type        = self::type();                    //获取栏目
        $this->config      = self::getConfig();               //获取配置文件
    }
    /**
     * @return \think\response\View
     * 网站首页
     */
    public function index(){
        $article = self::getArticle();
        return view('index',[
            'article'    => $article->items(),
            'page'       => $article->render(),
            'article_top'=> self::getTopArticle(),
            'tag'        => $this->tag,
            'type'       => $this->type,
            'config'     => $this->config,
            'reading_article' => $this->reading_article,
            'link'       => self::link()
        ]);
    }
    /**
     * @return \think\response\View
     * 网站栏目
     */
    public function category(){
        $type_id = intval(input('param.id'));
        if(empty($type_id)){
            $this->error('抱歉，栏目不存在!');
        }
        $type_seo = self::typeSEO($type_id);
        //判断栏目类型，根据栏目类型显示不同的模版
        if($type_seo['model_id'] == 1){
            $temp = 'category';
            $article  = self::getArticle(['type_id'=>$type_id]);//获取该栏目的信息
        }
        if($type_seo['model_id'] == 2){
            $ask = new \app\data\Data('ask');
            //$where = [],$order = '',$page = 20,$query = [],$filed = '*',$currentPage=1
            $article = $ask->pageSelf([],'create_time desc',20,input('param.'),'*',input('param.page'));
            $temp = 'message';
        }
        if($type_seo['model_id'] == 3){
            $temp = 'product';
            $article  = self::getProduct();
        }
        return view($temp,[
            'article'    => empty($article)  ? []  : $article->items(),
            'page'       => empty($article)  ? ''  : $article->render(),
            'type_seo'   => $type_seo,
            'reading_article' => $this->reading_article,
            'tag'        => $this->tag,
            'type'       => $this->type,
            'config'     => $this->config,
            'type_id'    => $type_id
        ]);
    }
    /**
     * @return \think\response\View
     * 文章详情页
     */
    public function article(){
        $article_id = intval(input('param.id'));
        if(empty($article_id)){
            $this->error('抱歉，文章不存在!');
        }
        self::_incReading($article_id);//点击一次给文章阅读数+1
        return view('article',[
            'one'              => self::getArticleOne($article_id),//获取当前文章
            'article_tag'      => self::getArticleTag($article_id),//获取文章标签
            'previous_article' => self::getArticleOne($article_id - 1),//获取上一篇文章
            'next_article'     => self::getArticleOne($article_id + 1), //获取下一篇文章
            'related_article'  => self::getRelatedArticle(self::getArticleTag($article_id),$article_id), //获取相关文章
            'reading_article' => $this->reading_article,
            'tag'              => $this->tag,
            'type'             => $this->type,
            'config'           => $this->config
        ]);
    }

    public function askArticle(){
        $ask_id = intval(input('param.id'));
        $ask = new \app\data\Data('ask');
        $one = $ask->find(['id' => $ask_id]);
        $ask->_setInc("id = $ask_id",'reading_num',1);
        return view('askArticle',[
            'one' => $one,
            'config'           => $this->config,
            'type'             => $this->type,
            'tag'              => $this->tag,
            'reading_article' => $this->reading_article,
        ]);
    }

    /**
     * @return \think\response\View
     * 根据标签获取文章内容
     */
    public function tagArticle(){
        $get = input('param.');
        if(empty($get['tag_id']) && empty($get['search'])){
            $this->error('抱歉，该标签或者关键词不存在!');
        }
        $article = empty($get['search']) ? self::getTagArticle($get) : self::getArticle(['search'=>$get['search']]);
        return view('tagArticle',[
            'reading_article' => $this->reading_article,
            'tag'              => $this->tag,
            'type'             => $this->type,
            'config'           => $this->config,
            'tag_article'      => $article->items(),
            'page'             => $article->render(),
            'tag_name'         => self::getTagOne(intval($get['tag_id'])),
            'search'           => $get['search'],
        ]);
    }

    public function ask(){
        try{
            if(request()->isPost()){
                $ask = new Data('ask');
                $post = input('param.');
                if(!captcha_check($post['code'])){
                    throw new \LogicException('验证码错误，请重新填写',10025);
                };
                unset($post['code']);
                if($ask->insertData($post)){
                    return json(['code'=>0,'msg'=>'操作成功，我会尽快帮助你！']);
                }else{
                    throw new \LogicException('遗憾，数据库操作失败',10020);
                }
            }
        }catch(\Exception $e){
            return json(['code'=>$e->getCode(),'msg'=>$e->getMessage()]);
        }
    }

}